﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ScreenManagement
{
    class StartScreen : MenuScreen
    {
        Texture2D startButton;

        #region Initialization

        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public StartScreen()
            : base("")
        {
            // Create a hidden menu entry to get the active user controller index
            MenuEntry hitStartButton = new MenuEntry("");

            // Hook up menu event handlers.
            hitStartButton.Selected += startButtonSelected;

            // Add entries to the menu.
            MenuEntries.Add(hitStartButton);
        }

        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;

            startButton = content.Load<Texture2D>("Textures/xboxControllerStart");

            base.LoadContent();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (IsActive)
            {
                //fadeTimer += gameTime.ElapsedGameTime.Milliseconds;

                //if (fadeTimer > timeToShow)
                //{
                //    this.ScreenManager.AddScreen(new MainMenuScreen(), null);
                //    this.ScreenManager.RemoveScreen(this);
                //}
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            
            ScreenManager.SpriteBatch.Draw(startButton, new Vector2((ScreenManager.GraphicsDevice.Viewport.Width / 2) - (startButton.Width / 2), ((ScreenManager.GraphicsDevice.Viewport.Height / 3) * 2) - (startButton.Height / 2)), Color.White);

            ScreenManager.SpriteBatch.End();

            base.Draw(gameTime);
        }

        #endregion

        #region Handle Input

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel(PlayerIndex playerIndex)
        {
            const string message = "Are you sure you want to exit the game?";

            MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox, playerIndex);
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.Game.Exit();
        }

        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void startButtonSelected(object sender, PlayerIndexEventArgs e)
        {
            this.ScreenManager.AddScreen(new MainMenuScreen(), e.PlayerIndex);
            this.ScreenManager.RemoveScreen(this);
        }

        #endregion
    }
}
